﻿using Smokey.Client.Contexts;
using Smokey.Client.Queries;
using Smokey.Shared.Constants;
using Smokey.Shared.DataTransferObjects;
using System.Windows;
namespace Smokey.Client.Screens.Applications {
    internal class ViewApplicationViewModel : ViewModelBase {

        public ViewApplicationViewModel(ViewModelContext context, int? applicationId)
            : base(context) {
                LoadApplication(applicationId);
        }

        private ApplicationDto application = new ApplicationDto();
        public ApplicationDto Application {
            get {
                return application;
            }

            set {
                if (application != value) {
                    application = value;
                    OnPropertyChanged("Application");
                    OnPropertyChanged("CanScheduleRuns");
                }
            }
        }

        public bool CanScheduleRuns {
            get {
                return application.ApplicationId.HasValue;
            }
        }

        public void LoadApplication(int? applicationId) {
            if (applicationId.HasValue) {
                Application = ApplicationQueries.GetApplication(applicationId.Value);
            } else {
                Application = new ApplicationDto() {
                    Status = ApplicationStatusConstant.New,
                };
            }
        }

        public void UpdateChanges() {
            application.Validate();
            if (application.IsValid) {
                Application = ApplicationQueries.UpdateApplication(application);
                MessageBox.Show("Your changes have been saved.",
                    "Smokey");
            } else {
                MessageBox.Show("Unable to save due to validation errors.", "Smokey");
            }
        }
    }
}
